package ccuserinterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import datamodels.GlobalDataStore;
import datamodels.Manuscript;
import datamodels.Status;
import datamodels.User;
/**
 * Make decision on a manuscript. 
 * 
 * @author Rithy Chhen
 *
 */
@SuppressWarnings("serial")
public class DecisionFrame extends JFrame{
	GlobalDataStore my_data_store;
	Manuscript my_man;
	User my_user;
	String my_decision;
	JLabel my_current_status;
	JRadioButton my_accept; 
	JRadioButton my_reject;
	JRadioButton my_undecided;
	/**
	 * constructor.
	 * @param the_man the manuscript which PC will make decision on.
	 * @param the_data_store the data store will be use to write the content back to the file.
	 */
	public DecisionFrame(Manuscript the_man, User the_user, GlobalDataStore the_data_store) {
		my_data_store = the_data_store;
		my_man = the_man;
		my_user = the_user;
		setLayout(null);
		setVisible(true);
		setTitle("Make Decision: select a manuscript");
		setPreferredSize(new Dimension(500,400));
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setUpFrame();
		setResizable(false);
		pack();
	}
	/**
	 * set up the frame.
	 */
	private void setUpFrame() {

		//the log out button that will take u back to the main screen.
		JButton logOut = new JButton("Log Out");
		logOut.setBounds(410, 5 , 80 , 20);
		logOut.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub   
                int option = JOptionPane.showConfirmDialog(null,"Are you sure you want to log out?", 
                		"Confirm", JOptionPane.YES_NO_OPTION);
                if (option == 0) {
                	dispose(); 
                	new ConferenceCreatorStart();
                }
			}
		});
		add(logOut);
		//set bounds and display info .
		String author = "Author: " + my_man.getAuthor().getName();
		JLabel l_name = new JLabel(author);
		l_name.setBounds(25, 35, 200, 20);
		add(l_name);
		String title = "Title: " + my_man.getTitle();
		JLabel l_title = new JLabel(title);
		l_title.setBounds(25, 55, 200, 20);
		add(l_title);
		JTextArea text = new JTextArea();
		text.setText(my_man.getText());
		text.setLineWrap(true);
		text.setWrapStyleWord(true);
		text.setEditable(false);
		text.setBounds(5, 35, 200, 275);
		JPanel left_panel  = new JPanel(); 
		left_panel.setBackground(Color.WHITE);
		left_panel.add(text);
		JScrollPane left_pane = new JScrollPane(left_panel);
		left_pane.setBounds(15, 80, 225, 260);
		add(left_pane);
		
		//get the reivew.
		String reviewers = "Reviewers: ";
		String reviews ="";
		int counter = 1;
		System.out.println(my_man.getReviews().size());
		for (int i = 0; i < my_man.getReviews().size(); i++) {
			
			reviewers = reviewers + my_man.getReviews().get(i).getAuthorName() + ", ";
			reviews = reviews + "Review " + counter + ": " ;
			reviews = reviews + my_man.getReviews().get(i).getText() + ". ";
			counter++;
		}
		//the right panel. display the reviews.
		JLabel r_name = new JLabel(reviewers);
		r_name.setBounds(265, 35, 200, 20);
		add(r_name);
		JTextArea rev_field = new JTextArea();
		rev_field.setText(reviews);
		rev_field.setLineWrap(true);
		rev_field.setWrapStyleWord(true);
		rev_field.setEditable(false);
		rev_field.setBounds(5, 35, 200, 275);
		JPanel right_panel  = new JPanel(); 
		right_panel.setBackground(Color.WHITE);
		right_panel.add(rev_field);
		JScrollPane right_pane = new JScrollPane(right_panel);
		right_pane.setBounds(255, 80, 225, 260);
		add(right_pane);
		
		//for the bottom buttons, rejected, accepted and undecided(only one can be selected at a time).
		ButtonGroup choice = new ButtonGroup();
		my_accept = new JRadioButton("Accept");
		my_accept.setBounds(25, 350, 100, 20);
		my_accept.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(my_accept.isSelected()) {
					my_decision = my_accept.getText();
					System.out.println(my_decision);
				}
			}
			
		});
		my_reject = new JRadioButton("Reject");
		my_reject.setBounds(125, 350, 100, 20);
		my_reject.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				my_decision = my_reject.getText();
				System.out.println(my_decision);
			}
		});
		my_undecided = new JRadioButton("Undecide");
		my_undecided.setBounds(225, 350, 100, 20);
		my_undecided.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				my_decision = my_undecided.getText();
			}
		});
		//display the current status.
		String display_status = checkStatus(my_man.getCurrentStatus());
		my_current_status = new JLabel(display_status);
		my_current_status.setBounds(25, 5, 200, 20);
		add(my_current_status);
		choice.add(my_accept);
		choice.add(my_reject);
		choice.add(my_undecided);
		add(my_accept);
		add(my_reject);
		add(my_undecided);
		//change the status button and its actions.
		JButton go = new JButton("Change");
		go.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null, "status successfully changed");
				if (my_decision.equals("Accept")) {
					my_man.setCurrentStatus(Status.ACCEPT);
					
				} else if (my_decision.equals("Reject")) {
					my_man.setCurrentStatus(Status.REJECT);
				} else  {
					my_man.setCurrentStatus(Status.UNDECIDED);
				}
				String new_status = checkStatus(my_man.getCurrentStatus());
				my_current_status.setText(new_status);
				my_data_store.addConferenceObject(my_data_store.getCurrentConference());//save the data
				
			}		
		});
		go.setBounds(375, 350, 100, 20);
		add(go);
	}
	//check the current status.
	private String checkStatus(Status currentStatus) {
		String result = "Current Status: "; 
		if (currentStatus.equals(Status.ACCEPT)) {
			result = result + "ACCEPTED";
			my_decision = "Accept";
			my_accept.setSelected(true);
		} else if (currentStatus.equals(Status.REJECT)) {
			result = result + "REJECTED";
			my_decision = "Reject";
			my_reject.setSelected(true);
		} else {
			result = result + "UNDECIDED";
			my_undecided.setSelected(true);
			my_decision = "Undecided";
		}
		return result;
	}
}
